package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.sky.constant.StatusConstant.ENABLE;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {

        //设置分页页码，和页大小
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        //分页查询
        List<DishVO> dishVOList= dishMapper.pageQuery(dishPageQueryDTO);
        Page<DishVO> p = (Page<DishVO>) dishVOList;
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return Result.success(pageResult);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional
    public Result save(DishDTO dishDTO) {

        //拷贝属性
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDTO,dish);

        //设置状态
        dish.setStatus(ENABLE);

        //插入dish表
        dishMapper.save(dish);

        //获取数据库生成的菜品id
        Long id = dish.getId();

        //获取菜品的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()){
            saveFlavors(flavors, id);
        }
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public Result<DishVO> selectById(Long id) {

        //查询菜品
        DishVO dishVO = dishMapper.selectById(id);

        //查询口味,并设置口味属性
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(flavors);

        return Result.success(dishVO);
    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result setStatus(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);
        return Result.success();
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional
    public Result update(DishDTO dishDTO) {

        //修改数据库菜品
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        //删除原有口味
        Long dishId = dishDTO.getId();
        dishFlavorMapper.deleteByDishId(dishId);

        //新赠口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()){
            saveFlavors(flavors, dishId);
        }
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(List<Long> ids) {

        //删除菜品
        dishMapper.deleteByIds(ids);

        //删除菜品对应的口味
        dishFlavorMapper.deleteByDishIds(ids);

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public Result<List<Dish>> selectByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.selectByCategoryId(categoryId,null);
        return Result.success(dishList);
    }

    /**
     * 新增口味
     * @param flavors
     * @param dishId
     * @return
     */
    private void saveFlavors(List<DishFlavor> flavors, Long dishId) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.save(flavors);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.selectByCategoryId(dish.getCategoryId(),dish.getStatus());
        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
